//
//  SCStoreGoodsListController.m
//  shangcheng
//
//  Created by 张艳江 on 2019/6/21.
//  Copyright © 2019 张艳江. All rights reserved.
//

#import "SCStoreGoodsListController.h"
#import "SCStoreGoodsCell.h"
#import "SCGoodsDetailController.h"

@interface SCStoreGoodsListController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray   *dataArray;

@end

@implementation SCStoreGoodsListController

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = BGColor;
    
    //设置表格
    [self setUpCollectionView];
    
    //请求数据
    [self requestData];
}
- (void)setUpCollectionView{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(12, 12, 12, 12);
    
    CGFloat width = (kScreenW-34)/2;
    CGFloat height = width*131/175+64;
    layout.itemSize = CGSizeMake(width, height);
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH - kTopHeight - 35 - 44) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = BGColor;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"SCStoreGoodsCell" bundle:nil] forCellWithReuseIdentifier:@"SCStoreGoodsCellID"];
    [self.view addSubview:self.collectionView];
}
#pragma mark  --------UICollectionVIew 的代理方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    SCStoreGoodsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SCStoreGoodsCellID" forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    SCModel *model = self.dataArray[indexPath.row];
    SCGoodsDetailController *vc = [[SCGoodsDetailController alloc]init];
    vc.goodsId = model.goods_id;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - 请求数据
- (void)requestData{
    
    NSDictionary *dict;
    NSString *url;
    if (self.isSearch == YES) {
        dict = @{@"store_id":self.store_id,
                 @"goods_name":self.goods_name,
                 @"size":@"10",
                 @"pagenow":@(1)};
        url = [MyTool splicingWithUrl:@"admin.php?g=System&c=StoreApp&a=App_search"];
    }else{
        dict = @{@"store_id":self.store_id,
                 @"cat_id":self.cat_id,
                 @"size":@"10",
                 @"pagenow":@(1)};
        url = [MyTool splicingWithUrl:@"admin.php?g=System&c=StoreApp&a=category_goods"];
    }
    [SVProgressHUD showWithStatus:@"加载中..."];
    [[NetworkRequestTool sharedNetworkTool] POST:url parameters:dict finished:^(id result, NSString *error) {
        [SVProgressHUD dismiss];
        NSArray *tempArray;
        if (self.isSearch == YES) {
            tempArray = [SCModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"data"]];
        }else{
            tempArray = [SCModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"list"]];
        }
        [self.dataArray addObjectsFromArray:tempArray];
        [self.collectionView reloadData];
        NSLog(@"===%@",result);
    }];
}


@end
